home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2006 May / PCpro_2006_05.ISO / files / mobile / fma-2.0-stable-setup.exe / {app} / source / uOutlookSync.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2004-07-25  |  8.6 KB  |  316 lines

  1. unit uOutlookSync;
  2.  
  3. {
  4. *******************************************************************************
  5. * Descriptions: Outlook Contact Sync Unit
  6. * $Source: /cvsroot/fma/fma/uOutlookSync.pas,v $
  7. * $Locker:  $
  8. *
  9. * Todo:
  10. *
  11. * Change Log:
  12. * $Log: uOutlookSync.pas,v $
  13. * Revision 1.5  2004/07/25 13:30:37  lordlarry
  14. * Added the ability to select the Outlook folder where all the New contacts end up.
  15. *
  16. * Revision 1.4  2004/06/25 18:27:09  lordlarry
  17. * Added this changelog header
  18. *
  19. *
  20. }
  21.  
  22. interface
  23.  
  24. uses
  25.   uContactSync, Outlook8, Classes;
  26.  
  27. type
  28.   TOutlookContact = class(TContact)
  29.   private
  30.     FOutlookContact: ContactItem;
  31.   protected
  32.     function Exists: Boolean; override;
  33.   public
  34.     property OutlookContact: ContactItem read FOutlookContact write FOutlookContact;
  35.   end;
  36.  
  37.   TOutlookContactSource = class(TContactSource)
  38.   private
  39.     Outlook: OutlookApplication;
  40.     NmSpace: NameSpace;
  41.     FCategories: TStrings;
  42.     FFolders: TStrings;
  43.     FNewContactsFolder: String;
  44.     FNewContactsFolderFolder: MAPIFolder;
  45.     function InCategories(OutlookContact: ContactItem): Boolean;
  46.     procedure SetCategories(const Value: TStrings);
  47.     procedure SetFolders(const Value: TStrings);
  48.     procedure SetNewContactsFolder(const Value: String);
  49.   protected
  50.     function GetName: String; override;
  51.     function GetOutlookCategories: String;
  52.     function ExtractQuotedStr(Str: String): String;
  53.     procedure Read(Contact: TOutlookContact; OutlookContact: ContactItem);
  54.   public
  55.     constructor Create;
  56.     destructor Destroy; override;
  57.  
  58.     function New: TContact; override;
  59.     function Add(Value: TContact): TContact; override;
  60.     procedure Update(Contact, Value: TContact); override;
  61.     procedure Delete(Contact: TContact); override;
  62.  
  63.     procedure Load; override;
  64.  
  65.     property Categories: TStrings read FCategories write SetCategories;
  66.     property Folders: TStrings read FFolders write SetFolders;
  67.     property NewContactsFolder: String read FNewContactsFolder write SetNewContactsFolder;
  68.   end;
  69.  
  70. implementation
  71.  
  72. uses
  73.   SysUtils, Forms;
  74.  
  75. { TOutlookContactSource }
  76.  
  77. function TOutlookContactSource.GetOutlookCategories: String;
  78. var I: Integer;
  79. begin
  80.   Result := '';
  81.   for I := 0 to Categories.Count - 1 do
  82.     if Trim(Categories[I]) <> '' then begin
  83.       if Result <> '' then Result := Result + '; ';
  84.       Result := Result + Categories[I];
  85.     end;
  86. end;
  87.  
  88. function TOutlookContactSource.Add(Value: TContact): TContact;
  89. var
  90.   Contact: TOutlookContact;
  91. begin
  92.   Contact := New as TOutlookContact;
  93.   Contact.Clone(Value);
  94.   Contact.LinkedContact := Value;
  95.   Value.LinkedContact := Contact;
  96.   Contacts.Add(Contact);
  97.  
  98.   if Assigned(FNewContactsFolderFolder) then
  99.     Contact.OutlookContact := FNewContactsFolderFolder.Items.Add(olContactItem) as ContactItem
  100.   else
  101.     Contact.OutlookContact := Outlook.CreateItem(olContactItem) as ContactItem;
  102.  
  103.   with Contact.OutlookContact do begin
  104.     Title := Contact.Title;
  105.     FirstName := Contact.Name;
  106.     LastName := Contact.SurName;
  107.     CompanyName := Contact.Organization;
  108.     Email1Address := Contact.Email;
  109.     HomeTelephoneNumber := Contact.HomePhone;
  110.     BusinessTelephoneNumber := Contact.WorkPhone;
  111.     MobileTelephoneNumber := Contact.CellPhone;
  112.     HomeFaxNumber := Contact.FaxPhone;
  113.     OtherTelephoneNumber := Contact.OtherPhone;
  114.  
  115.     Categories := GetOutlookCategories;
  116.  
  117.     Save;
  118.  
  119.     Contact.ID := EntryID;
  120.   end;
  121.  
  122.   Result := Contact;
  123. end;
  124.  
  125. constructor TOutlookContactSource.Create;
  126. begin
  127.   inherited;
  128.   FCategories := TStringList.Create;
  129.   FCategories.Delimiter := ';';
  130.   FFolders := TStringList.Create;
  131.  
  132.   Outlook := CoOutlookApplication.Create;
  133.   NmSpace := Outlook.GetNamespace('MAPI');
  134. //  NmSpace.Logon('', '', False, False);
  135. end;
  136.  
  137. procedure TOutlookContactSource.Delete(Contact: TContact);
  138. begin
  139.   with Contact as TOutlookContact do begin
  140.     OutlookContact.Delete;
  141.  
  142.     OutlookContact := nil;
  143.   end;
  144. end;
  145.  
  146. destructor TOutlookContactSource.Destroy;
  147. begin
  148.   FCategories.Free;
  149.   FFolders.Free;
  150.   
  151.   inherited;
  152. end;
  153.  
  154. function TOutlookContactSource.ExtractQuotedStr(Str: String): String;
  155. var P: PChar;
  156. begin
  157.   P := PChar(Str);
  158.   Result := AnsiExtractQuotedStr(P, '"');
  159.   if Result = '' then Result := Str;
  160. end;
  161.  
  162. function TOutlookContactSource.GetName: String;
  163. begin
  164.   Result := 'Outlook';
  165. end;
  166.  
  167. function TOutlookContactSource.InCategories(OutlookContact: ContactItem): Boolean;
  168. var Cats, Cat: String;
  169.     P: Integer;
  170. begin
  171.   if Categories.Count > 0 then begin
  172.     Result := False;
  173.     Cats := OutlookContact.Categories;
  174.     while Cats <> '' do begin
  175.       P := Pos(';', Cats);
  176.       if P = 0 then  // A propper Outlook Version check would be better
  177.         P := Pos(',', Cats);  // Outlook 2003 uses , instead of ;
  178.       if P = 0 then
  179.         P := Length(Cats) + 1;
  180.  
  181.       Cat := Trim(Copy(Cats, 1, P - 1));
  182.       System.Delete(Cats, 1, P);
  183.  
  184.       Result := Categories.IndexOf(Cat) <> - 1;
  185.       if Result then Break;
  186.     end;
  187.   end
  188.   else
  189.     Result := True;
  190. end;
  191.  
  192. procedure TOutlookContactSource.Load;
  193.   procedure LoadFolder(Folder: MAPIFolder);
  194.   var I: Integer;
  195.       OutlookContact: ContactItem;
  196.       Contact: TOutlookContact;
  197.       Count, CountNew, CountFiltered: Integer;
  198.   begin
  199.     Count := 0;
  200.     CountNew := 0;
  201.     CountFiltered := 0;
  202.  
  203.     for I := 1 to Folder.Items.Count do
  204.       if Supports(Folder.Items.Item(I), ContactItem, OutlookContact) then begin
  205.         if InCategories(OutlookContact) then begin
  206.           Contact := Contacts.FindByID(OutlookContact.EntryID) as TOutlookContact;
  207.  
  208.           if Assigned(Contact) then begin
  209.             Contact.OutlookContact := OutlookContact;
  210.           end
  211.           else begin
  212.             Contact := New as TOutlookContact;
  213.             Contact.ID := OutlookContact.EntryID;
  214.             Contact.SyncHash := Contact.Hash;
  215.             Contact.OutlookContact := OutlookContact;
  216.             Contacts.Add(Contact);
  217.  
  218.             Inc(CountNew);
  219.           end;
  220.  
  221.           Read(Contact, OutlookContact);
  222.   
  223.           Inc(Count);
  224.         end
  225.         else
  226.           Inc(CountFiltered);
  227.  
  228.         Application.ProcessMessages;
  229.       end;
  230.  
  231.     SyncLogFmt('Loaded %d contacts (%d new, %d filtered out) from %s[%s]', [Count, CountNew, CountFiltered, Name, Folder.Name]);
  232.   end;
  233.  
  234. var I: Integer;
  235.     Folder: MAPIFolder;
  236. begin
  237.   if FFolders.DelimitedText = 'DEFAULT' then begin
  238.     Folder := NmSpace.GetDefaultFolder(olFolderContacts);
  239.     if Assigned(Folder) then
  240.       FFolders.DelimitedText := Folder.EntryID;
  241.   end;
  242.  
  243.   for I := 0 to FFolders.Count - 1 do begin
  244.     Folder := NmSpace.GetFolderFromID(FFolders[I], '');
  245.     if Assigned(Folder) then
  246.       LoadFolder(Folder);
  247.   end;
  248. end;
  249.  
  250. function TOutlookContactSource.New: TContact;
  251. begin
  252.   Result := TOutlookContact.Create(Self);
  253. end;
  254.  
  255. procedure TOutlookContactSource.Read(Contact: TOutlookContact; OutlookContact: ContactItem);
  256. begin
  257.   with OutlookContact do begin
  258.     Contact.Title := Title;
  259.     Contact.Name := FirstName;
  260.     Contact.SurName := LastName;
  261.     Contact.Organization := CompanyName;
  262.     Contact.Email := Email1Address;
  263.     Contact.HomePhone := DeformatPhoneNumber(HomeTelephoneNumber);
  264.     Contact.WorkPhone := DeformatPhoneNumber(BusinessTelephoneNumber);
  265.     Contact.CellPhone := DeformatPhoneNumber(MobileTelephoneNumber);
  266.     Contact.FaxPhone := DeformatPhoneNumber(HomeFaxNumber);
  267.     Contact.OtherPhone := DeformatPhoneNumber(OtherTelephoneNumber);
  268.   end;
  269. end;
  270.  
  271. procedure TOutlookContactSource.SetCategories(const Value: TStrings);
  272. begin
  273.   FCategories.Assign(Value);
  274. end;
  275.  
  276. procedure TOutlookContactSource.Update(Contact, Value: TContact);
  277. begin
  278.   with (Contact as TOutlookContact).OutlookContact do begin
  279.     Title := Value.Title;
  280.     FirstName := Value.Name;
  281.     LastName := Value.SurName;
  282.     CompanyName := Value.Organization;
  283.     Email1Address := Value.Email;
  284.     HomeTelephoneNumber := Value.HomePhone;
  285.     BusinessTelephoneNumber := Value.WorkPhone;
  286.     MobileTelephoneNumber := Value.CellPhone;
  287.     HomeFaxNumber := Value.FaxPhone;
  288.     OtherTelephoneNumber := Value.OtherPhone;
  289.  
  290.     Save;
  291.   end;
  292. end;
  293.  
  294. procedure TOutlookContactSource.SetFolders(const Value: TStrings);
  295. begin
  296.   FFolders.Assign(Value);
  297. end;
  298.  
  299. procedure TOutlookContactSource.SetNewContactsFolder(const Value: String);
  300. begin
  301.   if FNewContactsFolder <> Value then begin
  302.     FNewContactsFolder := Value;
  303.  
  304.     FNewContactsFolderFolder := NmSpace.GetFolderFromID(FNewContactsFolder, '');
  305.   end;
  306. end;
  307.  
  308. { TOutlookContact }
  309.  
  310. function TOutlookContact.Exists: Boolean;
  311. begin
  312.   Result := Assigned(FOutlookContact);
  313. end;                                             
  314.  
  315. end.
  316.